package com.example.ktapplication.model

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.ktapplication.bean.Media
import com.example.ktapplication.bean.Navi
import com.example.ktapplication.bean.Project
import com.example.ktapplication.bean.Sys
import com.example.ktapplication.bean.SysBlog
import com.example.ktapplication.bean.musicData
import com.example.ktapplication.bean.videoData
import com.example.ktapplication.enum.StateLayoutMode
import com.example.ktapplication.net.NetRepository
import com.example.ktapplication.utils.httpRequest
import com.example.ktapplication.utils.ioSwitchMain
import kotlinx.coroutines.delay

class SystemViewModel : ViewModel() {

    val naviData by lazy {
        MutableLiveData<MutableList<Navi>>()
    }

    val sysData by lazy {
        MutableLiveData<MutableList<Sys>>()
    }

    val sysBlogData by lazy {
        MutableLiveData<SysBlog>()
    }

    val projectData by lazy {
        MutableLiveData<MutableList<Project>>()
    }

    val mediaData by lazy {
        MutableLiveData<MutableList<Media>>()
    }

    val reqState by lazy {
        MutableLiveData<StateLayoutMode>()
    }

    fun getNavi() {
        httpRequest({
            return@httpRequest NetRepository.instance.service.getNavi()
        }, {
            resultState(it)
            naviData.value = it
        }, { code, _ ->
            code.errorState()
        }, {

        })
    }

    fun getSys() {
        loadState()
        httpRequest({
            return@httpRequest NetRepository.instance.service.getSys()
        }, {
            resultState(it)
            sysData.value = it
        }, { code, _ ->
            code.errorState()
        }, {

        })
    }

    fun getSysBlog(cid: Int) {
        loadState()
        httpRequest({
            return@httpRequest NetRepository.instance.service.getSysBlogList(cid)
        }, {
            resultState(it)
            sysBlogData.value = it
        }, { code, _ ->
            code.errorState()
        }, {

        })
    }

    fun getProject() {
        loadState()
        httpRequest({
            return@httpRequest NetRepository.instance.service.getProject()
        }, {
            resultState(it)
            projectData.value = it
        }, { code, _ ->
            code.errorState()
        }, {

        })
    }

    fun getProjectBlog(cid: Int) {
        loadState()
        httpRequest({
            return@httpRequest NetRepository.instance.service.getProjectBlogList(cid)
        }, {
            resultState(it)
            sysBlogData.value = it
        }, { code, _ ->
            code.errorState()
        }, {

        })
    }

    fun collect(cid: Int, action: (Boolean) -> Unit = {}) {
        httpRequest({
            return@httpRequest NetRepository.instance.service.collect(cid)
        }, {
            action.invoke(true)
        }, { _, _ ->
            action.invoke(false)
        }, {

        })
    }

    fun unCollect(cid: Int, action: (Boolean) -> Unit = {}) {
        httpRequest({
            return@httpRequest NetRepository.instance.service.unCollect(cid)
        }, {
            action.invoke(true)
        }, { _, _ ->
            action.invoke(false)
        }, {

        })
    }

    private fun <T> resultState(t: T) {
        if (t == null) {
            reqState.value = StateLayoutMode.EMPTY
        } else {
            reqState.value = StateLayoutMode.CONTENT
        }
    }

    private fun Int.errorState() {
        if (this == -1) {
            reqState.value = StateLayoutMode.NET
        } else {
            reqState.value = StateLayoutMode.ERROR
        }
    }

    private fun loadState() {
        reqState.value = StateLayoutMode.LOADING
    }

    companion object {
        const val MEDIA_REQ_MUSIC = 0
        const val MEDIA_REQ_VIDEO = 1
    }

    fun getMediaData(mediaType: Int = MEDIA_REQ_MUSIC) {
        loadState()
        ioSwitchMain({
            delay(2000)
            if (mediaType == MEDIA_REQ_MUSIC) {
                musicData
            } else {
                videoData
            }
        }, {
            resultState(StateLayoutMode.CONTENT)
            mediaData.value = it
        })
    }
}